package com.crk.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.crk.entity.Hospitals;
import com.crk.service.IHospitalsService;
import com.crk.util.JwtUtil;
import com.crk.util.RestResult;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author com.crk
 * @since 2021-03-30
 */
@RestController
@CrossOrigin
@RequestMapping("/hospitals")
public class HospitalsController {

	@Autowired
	private IHospitalsService hospitalsService;
	@Autowired
	private JwtUtil jwtUtil;
	
	@RequestMapping("/getHosName")
	public RestResult<Hospitals> getHospitalName(){
		List<Hospitals> list = hospitalsService.list();
		return RestResult.ok(list);
	}
	
	
	@RequestMapping("/login")
	public RestResult<Hospitals> HosLogin(Hospitals hospital){
		Hospitals hos = hospitalsService.getById(hospital.getHospitalId());
		if(hos!=null&&hospital.getHospitalName().equals(hos.getHospitalName())&&hospital.getHospitalId().equals(hos.getHospitalId())&&hospital.getHospitalPassword().equals(hos.getHospitalPassword())) {
			String jwt = jwtUtil.createJwtHos(hos);
			return RestResult.ok(hos,jwt);
		}else {
			return RestResult.failed("账号或者密码错误！");
		}
	}
	
	
	@RequestMapping("/getLow")
	public RestResult<Hospitals> getLowHosInfo(Integer hospitalId){
		Hospitals hos = hospitalsService.getById(hospitalId);
		int ownlevel=hos.getHospitalLevel();
		List<Hospitals> lowhos = hospitalsService.selectLowHos(ownlevel);
		return RestResult.ok(lowhos);
	}
	
	@RequestMapping("/getSen")
	public RestResult<Hospitals> getSenHos(Integer hospitalId){
		Hospitals hos = hospitalsService.getById(hospitalId);
		int ownlevel=hos.getHospitalLevel();
		List<Hospitals> senhos = hospitalsService.selectSenHos(ownlevel);
		return RestResult.ok(senhos);
	}
}

